clear
set more off
cap log cl

use ${wd}/Tenure_supplement_extract.dta if age >= 20 & age <= 65, clear

drop if year == 1983

// job tenure

keep if !missing(jtyears)
drop if jtyears == 99.99 // refused
drop if jtyears == 99.98 // no response

// union

label drop union_lbl
drop if union == 0
recode union 1=0 
recode union (2 3 = 1)

// industries

g ind_90=ind1990
do ${CleaningCPS}/programs/ind_90_des_crosswalk
do ${CleaningCPS}/programs/ind_des_major_recodes

// educ

	gen x = 1 if educ <=60
	replace x = 2 if inrange(educ, 71,73)
	replace x = 3 if inrange(educ, 80,100)
	replace x = 4 if inrange(educ, 110, 125)
	drop educ
	rename x educ
	
// adjusted average tenure

gen age_2 = age*age
gen female = sex == 2
gen black = race == 200

// year variable to match main data.
gen year2=0
replace year2=1 if year>=1978 & year<=1980
replace year2=2 if year>=1987 & year<=1990
replace year2=3 if year>=1998 & year<=2000
replace year2=4 if year>=2008 & year<=2010
replace year2=5 if year>=2018 & year<=2020


cap mat drop delta
forval y = 2/5 {
	reg jtyears i.educ##(black c.age c.age_2 female ) ///
				   c.age#(black  female )                ///
				   c.age_2#(black  female ) i.union ///
				   [aw = jtsuppwt ] if year2==`y', robust
				   
				   
margins union, at( age=40 female = 0 educ = 2 black = 0 )

mat g = r(table)
mat g = g[1,1...]
mat delta = nullmat(delta) \ g
}

// since we don't observe union status befor 1998 in IPUMS
// we use 1987 for both year 1 and 2
mat delta = delta[1,1...] \ delta  

mat list delta


use ${wd}/Tenure_supplement_extract.dta if age >= 20 & age <= 65, clear

// union

label drop union_lbl
drop if union == 0
recode union 1=0 
recode union (2 3 = 1)

gen jobloss = dwreas
recode jobloss (1/4=1) (5=2) (6=3)

label define jobloss_lbl ///
1 "layoff" ///
2 "self-operation closed" ///
3 "Other" 

label values jobloss jobloss_lbl
